import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { User } from '../../models/User';
import { Loginauth } from '../../service/loginauth';
import { UserService } from '../../service/userService';
import { Message } from '../../models/Message';
import {tap} from 'rxjs/operators';

declare var $: any;
@Component({
  templateUrl: './messageinfo.html',
  styleUrls: ['./messageinfo.css']
})
export class MessageinfoComponent implements OnInit {
  isAdmin: boolean;
  page = 1;
  pageSize = 10;
  messages: Message[];
  total: number;
  constructor(private route: ActivatedRoute, private loginauth: Loginauth, private userService: UserService) {

  }
  ngOnInit() {
    this.loginauth.checkAdmin().subscribe((isadmin) => {
      this.isAdmin = isadmin;
    })
    this.update(this.page).subscribe();
  }

  update(page) {
    return this.userService.getmsgs(page, this.pageSize).pipe(tap((res) => {
      this.messages = res.res;
      this.total = res.total;
      this.page = page;
    }))
  }

  onPageChanged(page: number) {
    this.page = page;
    this.update(page).subscribe();
  }

  trackById(index: number, item: any) {
    return item.id;
  }
}
